package com.uenpay.im.zxing.encoding;

import java.util.Hashtable;

import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.uenpay.util.common.PicUtil;

/**
 * @author Ryan Tang
 */
public final class EncodingHandler {
    private static final int BLACK = 0xff000000;

    public static Bitmap createQRCode(String str, int widthAndHeight)
            throws WriterException {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix matrix = new MultiFormatWriter().encode(str,
                BarcodeFormat.QR_CODE, widthAndHeight, widthAndHeight);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = BLACK;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    // 生成一张带logo的二维码图片
    public static Bitmap createCodeWithLogo(Bitmap bitmap_qr, Bitmap bitmap_logo) {

        if (bitmap_qr == null || bitmap_logo == null) {
            return bitmap_qr;
        }

        // 二维码和logo合并
        Bitmap bitmap = Bitmap.createBitmap(bitmap_qr.getWidth(), bitmap_qr.getHeight(), bitmap_qr.getConfig());
        Canvas canvas = new Canvas(bitmap);

        // 二维码
        if (bitmap_qr != null && !bitmap_qr.isRecycled()) {
            canvas.drawBitmap(bitmap_qr, 0, 0, null);
        }

        // 将图片缩放到二维码图片的 1/6
        // Zxing最大容错为 1/5
        bitmap_logo = PicUtil.zoomBitmap(bitmap_logo, bitmap_qr.getWidth() / 6, bitmap_qr.getHeight() / 6);

        // logo绘制在二维码中央
        canvas.drawBitmap(bitmap_logo, bitmap_qr.getWidth() / 2 - bitmap_logo.getWidth() / 2,
                bitmap_qr.getHeight() / 2 - bitmap_logo.getHeight() / 2, null);

        bitmap_qr.recycle();
        bitmap_logo.recycle();
        return bitmap;
    }

    //生成条形码
    public static Bitmap CreateOneDCode(String content, int w, int h) throws WriterException {
        // 生成一维条码,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
        BitMatrix matrix = new MultiFormatWriter().encode(content,
                BarcodeFormat.CODE_128, w, h);
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = BLACK;
                }
            }
        }
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

}